#include "../head/demoudleform.h"
#include "ui_demoudleform.h"
#include <QtCharts>
#include <QtCharts/QChartView>
#include <QtCharts/QChart>
#include <QtCharts/QLegend>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCore/QTime>
#include <QtWidgets/QGridLayout>
#include <QtCharts/QScatterSeries>
#include "../head/mainwindow.h"

extern int flag;


DemoudleForm::DemoudleForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DemoudleForm)
{
    ui->setupUi(this);

    this->setWindowTitle("均衡后误差");

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_1.append(i);
        y_1.append(mutiSignalDecode->EqSig_PriCh_Temp_I[HIGH][i]);
    }
    curGraph_1 = ui->customPlot_1->addGraph();
    curGraph_1->setLineStyle(QCPGraph::lsNone);
    curGraph_1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));
    ui->customPlot_1->plotLayout()->insertRow(0);
    m_title_1 = new QCPTextElement(ui->customPlot_1, "Re");
    ui->customPlot_1->plotLayout()->addElement(0, 0, m_title_1);
    curGraph_1->setData(x_1, y_1);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_1->graph(0)->rescaleAxes();
    ui->customPlot_1->legend->setVisible(false); // 显示图例
    ui->customPlot_1->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_2.append(i);
        y_2.append(mutiSignalDecode->EqSig_PriCh_Temp_Q[HIGH][i]);
    }
    curGraph_2 = ui->customPlot_2->addGraph();
    curGraph_2->setLineStyle(QCPGraph::lsNone);
    curGraph_2->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));
    ui->customPlot_2->plotLayout()->insertRow(0);
    m_title_2 = new QCPTextElement(ui->customPlot_2, "Im");
    ui->customPlot_2->plotLayout()->addElement(0, 0, m_title_2);
    curGraph_2->setData(x_2, y_2);

    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_2->graph(0)->rescaleAxes();
    ui->customPlot_2->legend->setVisible(false); // 显示图例
    ui->customPlot_2->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_3.append(i);
        y_3.append(mutiSignalDecode->EqErr_PriCh_Temp[HIGH][i]);
    }
    curGraph_3 = ui->customPlot_3->addGraph();
    curGraph_3->setLineStyle(QCPGraph::lsLine);
    ui->customPlot_3->graph(0)->setPen(QPen(Qt::magenta)); // 曲线的颜色
    ui->customPlot_3->plotLayout()->insertRow(0);
    m_title_3 = new QCPTextElement(ui->customPlot_3, "均衡后误差");
    ui->customPlot_3->plotLayout()->addElement(0, 0, m_title_3);
    curGraph_3->setData(x_3, y_3);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_3->graph(0)->rescaleAxes();
    ui->customPlot_3->legend->setVisible(false); // 显示图例
    ui->customPlot_3->replot();

    for (int i=0; i<MseqCorrOut_PriCh_TempSize; i++)
    {
        x_4.append(i);
        y_4.append(mutiSignalDecode->MseqCorrOut_PriCh_Temp[HIGH][i]);
    }
    curGraph_4 = ui->customPlot_4->addGraph();
    curGraph_4->setLineStyle(QCPGraph::lsLine);
    ui->customPlot_4->graph(0)->setPen(QPen(Qt::magenta)); // 曲线的颜色
    ui->customPlot_4->plotLayout()->insertRow(0);
    m_title_4 = new QCPTextElement(ui->customPlot_4, "M序列相关");
    ui->customPlot_4->plotLayout()->addElement(0, 0, m_title_4);
    curGraph_4->setData(x_4, y_4);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_4->graph(0)->rescaleAxes();
    ui->customPlot_4->legend->setVisible(false); // 显示图例
    ui->customPlot_4->replot();


    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_5.append(i);
        y_5.append(mutiSignalDecode->EqSig_PriCh_Temp_I[LOW][i]);
    }
    curGraph_5 = ui->customPlot_5->addGraph();
    curGraph_5->setLineStyle(QCPGraph::lsNone);
    curGraph_5->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));
    ui->customPlot_5->plotLayout()->insertRow(0);
    m_title_5 = new QCPTextElement(ui->customPlot_5, "Re");
    ui->customPlot_5->plotLayout()->addElement(0, 0, m_title_5);
    curGraph_5->setData(x_5, y_5);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_5->graph(0)->rescaleAxes();
    ui->customPlot_5->legend->setVisible(false); // 显示图例
    ui->customPlot_5->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_6.append(i);
        y_6.append(mutiSignalDecode->EqSig_PriCh_Temp_Q[LOW][i]);
    }
    curGraph_6 = ui->customPlot_6->addGraph();
    curGraph_6->setLineStyle(QCPGraph::lsNone);
    curGraph_6->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));
    ui->customPlot_6->plotLayout()->insertRow(0);
    m_title_6 = new QCPTextElement(ui->customPlot_6, "Im");
    ui->customPlot_6->plotLayout()->addElement(0, 0, m_title_6);
    curGraph_6->setData(x_6, y_6);

    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_6->graph(0)->rescaleAxes();
    ui->customPlot_6->legend->setVisible(false); // 显示图例
    ui->customPlot_6->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_7.append(i);
        y_7.append(mutiSignalDecode->EqErr_PriCh_Temp[LOW][i]);
    }
    curGraph_7 = ui->customPlot_7->addGraph();
    curGraph_7->setLineStyle(QCPGraph::lsLine);
    ui->customPlot_7->graph(0)->setPen(QPen(Qt::magenta)); // 曲线的颜色
    ui->customPlot_7->plotLayout()->insertRow(0);
    m_title_7 = new QCPTextElement(ui->customPlot_7, "均衡后误差");
    ui->customPlot_7->plotLayout()->addElement(0, 0, m_title_7);
    curGraph_7->setData(x_7, y_7);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_7->graph(0)->rescaleAxes();
    ui->customPlot_7->legend->setVisible(false); // 显示图例
    ui->customPlot_7->replot();

    for (int i=0; i<MseqCorrOut_PriCh_TempSize; i++)
    {
        x_8.append(i);
        y_8.append(mutiSignalDecode->MseqCorrOut_PriCh_Temp[LOW][i]);
    }
    curGraph_8 = ui->customPlot_8->addGraph();
    curGraph_8->setLineStyle(QCPGraph::lsLine);
    ui->customPlot_8->graph(0)->setPen(QPen(Qt::magenta)); // 曲线的颜色
    ui->customPlot_8->plotLayout()->insertRow(0);
    m_title_8 = new QCPTextElement(ui->customPlot_8, "M序列相关");
    ui->customPlot_8->plotLayout()->addElement(0, 0, m_title_8);
    curGraph_8->setData(x_8, y_8);
    // 为坐标轴添加标签
    //ui->customPlot->xAxis->setLabel("In-Phase");
    //ui->customPlot->yAxis->setLabel("Quadrature");
    // 设置坐标轴的范围，以看到所有数据
    ui->customPlot_8->graph(0)->rescaleAxes();
    ui->customPlot_8->legend->setVisible(false); // 显示图例
    ui->customPlot_8->replot();

}

void DemoudleForm::initDataList(){

}

DemoudleForm::~DemoudleForm()
{
    delete ui;
}


//关闭窗口回到导航界面
 void DemoudleForm::closeEvent(QCloseEvent *e)
 {
     //nf->show();
 }

void DemoudleForm::FirFilter_2(double *b, int inputlength, double *x, QList<double> y)
{
    int i, j;
    y[0] = b[0] * x[0];
    for (i = 1; i < inputlength; i++)//对20个输入函数逐一处理
    {
        y[i] = 0.0;
        for (j = 0; j < 31; j++)
        {
            if (i >= j) {
                y[i] = y[i] + b[j] * x[i - j];
            }//分子系数与输入函数的乘积和
        }
    }
}

void DemoudleForm::drawHighWaveformPlot()
{
    int type = 1;
    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_1.replace(i,i);
        y_1.replace(i,mutiSignalDecode->EqSig_PriCh_Temp_I[type][i]);
    }
    curGraph_1->setData(x_1, y_1);
    ui->customPlot_1->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_1->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_2.replace(i,i);
        y_2.replace(i,mutiSignalDecode->EqSig_PriCh_Temp_Q[type][i]);
    }
    curGraph_2->setData(x_2, y_2);
    ui->customPlot_2->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_2->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_3.replace(i,i);
        y_3.replace(i,mutiSignalDecode->EqErr_PriCh_Temp[type][i]);
    }
    curGraph_3->setData(x_3, y_3);
    ui->customPlot_3->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_3->replot();

    for (int i=0; i<MseqCorrOut_PriCh_TempSize; i++)
    {
        x_4.replace(i,i);
        y_4.replace(i,mutiSignalDecode->MseqCorrOut_PriCh_Temp[type][i]);
    }
    curGraph_4->setData(x_4, y_4);
    ui->customPlot_4->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_4->replot();
}

void DemoudleForm::drawLowWaveformPlot()
{
    int type = 0;
    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_5.replace(i,i);
        y_5.replace(i,mutiSignalDecode->EqSig_PriCh_Temp_I[type][i]);
    }
    curGraph_5->setData(x_5, y_5);
    ui->customPlot_5->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_5->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_6.replace(i,i);
        y_6.replace(i,mutiSignalDecode->EqSig_PriCh_Temp_Q[type][i]);
    }
    curGraph_6->setData(x_6, y_6);
    ui->customPlot_6->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_6->replot();

    for (int i=0; i<EQ_ALL_ERR_LEN; i++)
    {
        x_7.replace(i,i);
        y_7.replace(i,mutiSignalDecode->EqErr_PriCh_Temp[type][i]);
    }
    curGraph_7->setData(x_7, y_7);
    ui->customPlot_7->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_7->replot();

    for (int i=0; i<MseqCorrOut_PriCh_TempSize; i++)
    {
        x_8.replace(i,i);
        y_8.replace(i,mutiSignalDecode->MseqCorrOut_PriCh_Temp[type][i]);
    }
    curGraph_8->setData(x_8, y_8);
    ui->customPlot_8->graph(0)->rescaleAxes();
    // 重画图像
    ui->customPlot_8->replot();
}

void DemoudleForm::showSignalInfo(double singnal,int hzType){
    QString strSig = QString::number(singnal,'f', 3);
    if(hzType == 1){
        ui->labelXZB->setText("信噪比 = "+ strSig);
    }else{
        ui->labelXZB_2->setText("信噪比 = "+ strSig);
    }

}
